﻿#include "MyGrid.h"


MyGrid::MyGrid(qreal mustWidth, QList<MyMainRole*>* rolelist, int form)
{
//    setFlags(QGraphicsItem::ItemIsSelectable);
    mWidth = mustWidth;
    setState(form);
    //启用拖动事件
    setAcceptDrops(true);

    list.append("mainRole");
    list.append("boxReq");
    list.append("box");
    list.append("wall");
    list.append("move");

    mrolelist = rolelist;
}

//设置格子在阵列的排序
void MyGrid::setPoint(int x, int y)
{
    mPoint = QPoint(x,y);
}

//0草地
//1墙体
void MyGrid::setState(int state)
{
    mState = state;
    QPixmap pixmap;
    switch (state) {
        case 0: pixmap.load(":/pixmap/PNG/Default size/Ground/ground_06.png"); break;
        case 1: pixmap.load(":/pixmap/PNG/Default size/Blocks/block_07.png"); break;
    }
    this->setPixmap(pixmap);
    int mapw = pixmap.height();
    this->setScale(mWidth/mapw);
}

int MyGrid::getState()
{
    return mState;
}

void MyGrid::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    QString text = event->mimeData()->text();

    int index = list.indexOf(text);
    //如果不是规定的指令就拒绝
    if(index == -1)
        event->ignore();
    else {
        //检查该位置有没有东西
//        qDebug() << "this->pos:" << this->pos();
        foreach(MyMainRole* role,*mrolelist)
        {//如果该位置有东西就拒绝
//            qDebug() << "role->pos:" << role->pos();
            if(role->pos() == this->pos())
            {
                event->ignore();
                return;
            }
        }
        event->accept();
    }

}

void MyGrid::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    //正常的运行
    QString text = event->mimeData()->text();
    int index = list.indexOf(text);
    if(index == -1)
    {

    }
    else {
        //如果是移动指令
        if(text == "move")
        {
            QPointer<MyMainRole> pot = moveRole;
            pot->setPos(this->pos());
            pot->setPoint(mPoint.x(),mPoint.y());
        }
        else {
            emit sendPoi(index,mPoint,false);
            event->accept();
        }
    }
}

